package com.cib.web;

import com.alibaba.fastjson.JSON;
import com.cib.entity.VerifyResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @描述:
 * @Author: zhangh
 * @Time: 2021/11/14 13:01
 **/
@RestController
public class ChannelController {
    @Value("#{${limit-parames}}")
    private Map<String,Integer> params;

    @Value("#{${limit.ltime}}")
    private Integer ltime;

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @GetMapping("asrauth")
    public VerifyResult asr(HttpServletRequest request){
        String channel = request.getParameter("channel");
        String token = request.getParameter("token");
        boolean channelflg = StringUtils.isBlank(channel);
        boolean tokenflg = StringUtils.isBlank(token);
        VerifyResult verifyResult = new VerifyResult();
        if (channelflg||tokenflg){
            verifyResult.setMsg(channelflg?"渠道号为空":"token值为空");
            return verifyResult;
        }

        if (channel=="sjyhapp"){
            //若是手机银行渠道，则不限流，直接开始鉴权逻辑，否则开始限流逻辑
            verifyResult= authFunction(token);
            return verifyResult;
        }
        if (params==null){
            verifyResult.setMsg("您未进行渠道限流配置，请配置正确后重启服务再试");
            verifyResult.setAppid("1");
            verifyResult.setErrno("3333");
            return verifyResult;
        }
        System.out.println(params);
        //若非手机银行渠道，需要进行鉴权限流处理
        Integer lcount = params.get(channel);
        boolean flag=isCurlimit(channel,ltime,lcount);
        if (!flag){
            verifyResult= authFunction(token);
            return verifyResult;
        }
        //说明既不是手机银行渠道，又超过了并发限制，限流处理
        verifyResult.setMsg(channel+"渠道超过并发次数，禁止鉴权！");
        verifyResult.setAppid("1");
        return verifyResult;


    }

    //限流逻辑

    /**
     *
     * @param channel  渠道
     * @param ltime    并发时间
     * @param lcount    并发次数
     * @return
     */
    private boolean isCurlimit(String channel, Integer ltime, Integer lcount) {
        long nowtimes = System.currentTimeMillis();
        //移除60s之前记录
        redisTemplate.opsForZSet().removeRangeByScore(channel, 0, nowtimes - ltime * 1000);
        //当前请求次数
        Long count = redisTemplate.opsForZSet().zCard(channel);
        System.out.println(count);
        if (count>=lcount){
            //超过并发限制次数不放行
            return false;
        }
        redisTemplate.opsForZSet().add(channel,nowtimes+"uuid",nowtimes);
        return true;

    }

    private VerifyResult authFunction(String token) {
        System.out.println("开始鉴权逻辑");
        System.out.println("鉴权成功");
        VerifyResult verifyResult = new VerifyResult();
        verifyResult.setMsg("鉴权逻辑成功！");
        verifyResult.setErrno("0");
        verifyResult.setAppid("0");
        return verifyResult;
    }


}
